<template>
  <view class="issue-coupon-container">
    <!-- 自定义导航栏（保持原组件，调整样式） -->
    <custom-navbar title="发放优惠券"></custom-navbar>

    <!-- 表单区域 -->
    <view class="form-container">
      <!-- 优惠券名称 -->
      <view class="form-item">
        <view class="form-label">优惠券名称</view>
        <input
          v-model="couponForm.name"
          placeholder="请输入优惠券名称"
          class="form-input"
        />
      </view>

      <!-- 优惠券类型（单选按钮） -->
      <view class="form-item">
        <view class="form-label">优惠券类型</view>
        <view class="radio-group">
          <view class="radio-item" @click="selectType('discount')">
            <view class="radio-circle" :class="{ active: couponForm.type === 'discount' }">
              <image v-if="couponForm.type === 'discount'" src="/static/组 6@3x.png" class="radio-check-icon" mode="aspectFit"></image>
            </view>
            <text class="radio-label">满减券</text>
          </view>
          <view class="radio-item" @click="selectType('percentage')">
            <view class="radio-circle" :class="{ active: couponForm.type === 'percentage' }">
              <image v-if="couponForm.type === 'percentage'" src="/static/组 6@3x.png" class="radio-check-icon" mode="aspectFit"></image>
            </view>
            <text class="radio-label">打折券</text>
          </view>
        </view>
      </view>

      <!-- 满减配置（仅满减券显示） -->
      <view class="form-item" v-if="couponForm.type === 'discount'">
        <view class="discount-config">
          <text class="config-text">满</text>
          <input
            v-model="couponForm.condition"
            type="number"
            placeholder="200"
            class="config-input-underline"
          />
          <text class="config-text">元减</text>
          <input
            v-model="couponForm.value"
            type="number"
            placeholder="20"
            class="config-input-underline"
          />
          <text class="config-text">元</text>
        </view>
      </view>

      <!-- 打折配置（仅打折券显示） -->
      <view class="form-item" v-if="couponForm.type === 'percentage'">
        <view class="discount-config">
          <text class="config-text">打</text>
          <input
            v-model="couponForm.value"
            type="number"
            placeholder="8"
            class="config-input-underline"
          />
          <text class="config-text">折</text>
        </view>
      </view>

      <!-- 发放开始时间 -->
      <view class="form-item">
        <view class="form-label">发放开始时间</view>
        <picker
          mode="date"
          :value="couponForm.startTime"
          @change="onStartTimeChange"
        >
          <view class="picker-view">
            <text class="picker-text">{{ couponForm.startTime || '2023-03-18' }}</text>
            <uni-icons type="arrowright" size="16" color="#999999"></uni-icons>
          </view>
        </picker>
      </view>

      <!-- 发放结束时间 -->
      <view class="form-item">
        <view class="form-label">发放结束时间</view>
        <picker
          mode="date"
          :value="couponForm.endTime"
          @change="onEndTimeChange"
        >
          <view class="picker-view">
            <text class="picker-text">{{ couponForm.endTime || '2023-03-18' }}</text>
            <uni-icons type="arrowright" size="16" color="#999999"></uni-icons>
          </view>
        </picker>
      </view>

      <!-- 投放数量 -->
      <view class="form-item">
        <view class="form-label">投放数量</view>
        <view class="input-with-unit">
          <input
            v-model="couponForm.totalCount"
            type="number"
            placeholder="请输入数量"
            class="form-input"
          />
          <text class="unit-text">张</text>
        </view>
      </view>

      <!-- 每人限领 -->
      <view class="form-item">
        <view class="form-label">每人限领</view>
        <view class="input-with-unit">
          <input
            v-model="couponForm.limitPerUser"
            type="number"
            placeholder="请输入数量"
            class="form-input"
          />
          <text class="unit-text">张</text>
        </view>
      </view>

      <!-- 使用有效期 -->
      <view class="form-item">
        <view class="form-label">使用有效期</view>
        <view class="date-range">
          <picker
            mode="date"
            :value="couponForm.validStartTime"
            @change="onValidStartTimeChange"
          >
            <view class="date-picker">
              <text class="date-text">{{ couponForm.validStartTime || '开始时间' }}</text>
              <uni-icons type="arrowright" size="16" color="#999999"></uni-icons>
            </view>
          </picker>
          <picker
            mode="date"
            :value="couponForm.validEndTime"
            @change="onValidEndTimeChange"
          >
            <view class="date-picker">
              <text class="date-text">{{ couponForm.validEndTime || '结束时间' }}</text>
              <uni-icons type="arrowright" size="16" color="#999999"></uni-icons>
            </view>
          </picker>
        </view>
      </view>
    </view>

    <!-- 确认投放按钮 -->
    <view class="action-section">
      <button class="confirm-btn" @click="submitCoupon" :disabled="!canSubmit">确认投放</button>
    </view>
  </view>
</template>

<script>
export default {
  components: {
    'custom-navbar': () => import('@/components/custom-navbar.vue')
  },
  data() {
    return {
      couponForm: {
        name: '',
        type: 'discount',
        value: '',
        condition: '',
        totalCount: '',
        limitPerUser: '',
        startTime: '',
        endTime: '',
        validStartTime: '',
        validEndTime: ''
      }
    }
  },
  computed: {
    canSubmit() {
      return this.couponForm.name.trim() && 
             this.couponForm.value && 
             this.couponForm.condition && 
             this.couponForm.totalCount && 
             this.couponForm.limitPerUser &&
             this.couponForm.startTime && 
             this.couponForm.endTime &&
             this.couponForm.validStartTime &&
             this.couponForm.validEndTime
    }
  },
  methods: {
    selectType(type) {
      this.couponForm.type = type
    },
    onStartTimeChange(e) {
      this.couponForm.startTime = e.detail.value
    },
    onEndTimeChange(e) {
      this.couponForm.endTime = e.detail.value
    },
    onValidStartTimeChange(e) {
      this.couponForm.validStartTime = e.detail.value
    },
    onValidEndTimeChange(e) {
      this.couponForm.validEndTime = e.detail.value
    },
    async submitCoupon() {
      if (!this.canSubmit) {
        uni.showToast({ title: '请填写完整信息', icon: 'none' })
        return
      }
      if (new Date(this.couponForm.startTime) >= new Date(this.couponForm.endTime)) {
        uni.showToast({ title: '结束时间必须晚于开始时间', icon: 'none' })
        return
      }
      if (parseInt(this.couponForm.totalCount) <= 0) {
        uni.showToast({ title: '发放数量必须大于0', icon: 'none' })
        return
      }
      if (parseFloat(this.couponForm.value) <= 0) {
        uni.showToast({ title: '券面值必须大于0', icon: 'none' })
        return
      }
      try {
        uni.showLoading({ title: '发布中...' })
        await new Promise(resolve => setTimeout(resolve, 2000))
        uni.hideLoading()
        uni.showToast({ title: '发布成功', icon: 'success' })
        setTimeout(() => { uni.navigateBack() }, 1500)
      } catch (error) {
        uni.hideLoading()
        uni.showToast({ title: '发布失败', icon: 'none' })
      }
    },
    goBack() {
      uni.navigateBack()
    }
  }
}
</script>


<style lang="scss" scoped>
.issue-coupon-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 120rpx;
  padding-top: calc(env(safe-area-inset-top) + 88rpx);
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    top: calc(env(safe-area-inset-top) + 88rpx);
    left: 0;
    right: 0;
    height: 600px;
    background-image: linear-gradient(
      to bottom,
      #2f81fa 0%, #2f81fa 33.33%,
      #f7f7f7 33.33%, #f7f7f7 100% 
    );
    background-repeat: no-repeat;
    z-index: 0;
  }
}

/* 表单容器 */
.form-container {
  position: relative;
  z-index: 1;
  background: #ffffff;
  margin: 20rpx;
  border-radius: 20rpx;
  padding: 40rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
}

.form-item {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;

  &:last-child {
    margin-bottom: 0;
  }

  .form-label {
    width: 200rpx;
    font-size: 28rpx;
    color: #333333;
    font-weight: 500;
    flex-shrink: 0;
  }

  .form-input {
    flex: 1;
    height: 80rpx;
    background: #ffffff;
    border-radius: 12rpx;
    padding: 0 20rpx;
    font-size: 28rpx;
    color: #333333;
    border: 1rpx solid #e0e0e0;
    font-weight: 400;
  }

  /* 单选按钮组 */
  .radio-group {
    flex: 1;
    display: flex;
    gap: 40rpx;
  }

  .radio-item {
    display: flex;
    align-items: center;
    gap: 12rpx;
  }

  .radio-circle {
    width: 36rpx;
    height: 36rpx;
    border: 2rpx solid #007AFF;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;

    &.active {
      background: #007AFF;
    }

    .radio-check {
      width: 12rpx;
      height: 12rpx;
      background: #ffffff;
      border-radius: 50%;
    }

    .radio-check-icon {
      width: 24rpx;
      height: 24rpx;
    }
  }

  .radio-label {
    font-size: 28rpx;
    color: #333333;
  }

  /* 满减配置 */
  .discount-config {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 4rpx;
    margin-top: 20rpx;
  }

  .config-text {
    font-size: 28rpx;
    color: #333333;
  }

  .config-input {
    width: 100rpx;
    height: 50rpx;
    background: #ffffff;
    border-radius: 8rpx;
    padding: 0 8rpx;
    font-size: 28rpx;
    color: #333333;
    border: 1rpx solid #e0e0e0;
    text-align: center;
    font-weight: 400;
  }

  .config-input-underline {
    width: 80rpx;
    height: 40rpx;
    background: transparent;
    border: none;
    border-bottom: 2rpx solid #333333;
    font-size: 28rpx;
    color: #333333;
    text-align: center;
    outline: none;
  }

  /* 带单位的输入框 */
  .input-with-unit {
    flex: 1;
    display: flex;
    align-items: center;
    background: #ffffff;
    border: 1rpx solid #e0e0e0;
    border-radius: 12rpx;
    padding: 0 20rpx;
    height: 80rpx;

    .form-input {
      flex: 1;
      font-size: 28rpx;
      color: #333333;
      height: 100%;
      border: none;
      background: transparent;
      font-weight: 400;
      padding: 0;
    }
  }

  .unit-text {
    font-size: 28rpx;
    color: #666666;
    margin-left: 8rpx;
    font-weight: 400;
  }

  /* 日期选择器 */
  .picker-view {
    flex: 1;
    height: 80rpx;
    background: #ffffff;
    border-radius: 12rpx;
    padding: 0 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border: 1rpx solid #e0e0e0;
  }

  .picker-text {
    font-size: 28rpx;
    color: #333333;
    font-weight: 400;
  }

  /* 日期范围选择 */
  .date-range {
    flex: 1;
    display: flex;
    gap: 16rpx;
  }

  .date-picker {
    flex: 1;
    height: 80rpx;
    background: #ffffff;
    border-radius: 12rpx;
    padding: 0 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border: 1rpx solid #e0e0e0;
  }

  .date-text {
    font-size: 28rpx;
    color: #999999;
    font-weight: 400;
  }
}

/* 操作按钮区域 */
.action-section {
  position: relative;
  z-index: 1;
  padding: 40rpx 20rpx;
}

.confirm-btn {
  width: calc(100% - 60rpx);
  height: 88rpx;
  background: #0a78ff;
  color: #ffffff;
  border-radius: 44rpx;
  font-size: 32rpx;
  font-weight: bold;
  border: none;
  margin: 0 30rpx;

  &:disabled {
    background: #cccccc;
    color: #999999;
  }
}
</style>

				//   condition: parseFloat(this.couponForm.condition),

				//   totalCount: parseInt(this.couponForm.totalCount),

				//   startTime: this.couponForm.startTime,

				//   endTime: this.couponForm.endTime,

				//   issueType: this.couponForm.issueType,

				//   targetUsers: this.couponForm.targetUsers

				// })

				

				// 模拟API调用

				await new Promise(resolve => setTimeout(resolve, 2000))

				

				uni.hideLoading()

				uni.showToast({

					title: '发布成功',

					icon: 'success'

				})

				

				// 返回上一页

				setTimeout(() => {

					uni.navigateBack()

				}, 1500)

			} catch (error) {

				uni.hideLoading()

				uni.showToast({

					title: '发布失败',

					icon: 'none'

				})

			}

		},

		

		// 返回

		goBack() {

			uni.navigateBack()

		}

	}

}

</script>



<style lang="scss" scoped>

.issue-coupon-container {

	min-height: 100vh;

	background: #f5f5f5;

}



// 蓝色头部

.header {

	height: 120rpx;

	background: #007AFF;

}



// 表单容器

.form-container {

	background: #ffffff;

	margin: -20rpx 20rpx 0;

	border-radius: 20rpx 20rpx 0 0;

	padding: 40rpx;

	

	.form-item {

		display: flex;

		align-items: center;

		margin-bottom: 40rpx;

		

		&:last-child {

			margin-bottom: 0;

		}

		

		.form-label {

			width: 200rpx;

			font-size: 28rpx;

			color: #333333;

			font-weight: 500;

			flex-shrink: 0;

		}

		

		.form-input {

			flex: 1;

			height: 80rpx;

			background: #ffffff;

			border-radius: 8rpx;

			padding: 0 20rpx;

			font-size: 28rpx;

			color: #333333;

			border: 1rpx solid #e0e0e0;

		}

		

		// 单选按钮组

		.radio-group {

			flex: 1;

			display: flex;

			gap: 60rpx;

			

			.radio-item {

				display: flex;

				align-items: center;

				gap: 16rpx;

				

				.radio-circle {

					width: 36rpx;

					height: 36rpx;

					border: 2rpx solid #e0e0e0;

					border-radius: 50%;

					display: flex;

					align-items: center;

					justify-content: center;

					

					&.active {

						border-color: #007AFF;

					}

					

					.radio-check {

						width: 18rpx;

						height: 18rpx;

						background: #007AFF;

						border-radius: 50%;

					}

				}

				

				.radio-label {

					font-size: 28rpx;

					color: #333333;

				}

			}

		}

		

		// 满减券配置

		.discount-config {

			flex: 1;

			display: flex;

			align-items: center;

			gap: 8rpx;

			margin-top: 20rpx;

			

			.config-text {

				font-size: 28rpx;

				color: #333333;

			}

			

			.config-input {

				width: 100rpx;

				height: 50rpx;

				background: #ffffff;

				border-radius: 6rpx;

				padding: 0 8rpx;

				font-size: 28rpx;

				color: #333333;

				border: 1rpx solid #e0e0e0;

				text-align: center;

			}

		}

		

		// 带单位的输入框

		.input-with-unit {

			flex: 1;

			display: flex;

			align-items: center;

			gap: 12rpx;

			

			.form-input {

				width: 200rpx;

			}

			

			.unit-text {

				font-size: 28rpx;

				color: #333333;

			}

		}

		

		// 日期选择器

		.picker-view {

			flex: 1;

			height: 80rpx;

			background: #ffffff;

			border-radius: 8rpx;

			padding: 0 20rpx;

			display: flex;

			align-items: center;

			justify-content: space-between;

			border: 1rpx solid #e0e0e0;

			

			.picker-text {

				font-size: 28rpx;

				color: #333333;

			}

			

			.picker-arrow {

				width: 20rpx;

				height: 20rpx;

				transform: rotate(90deg);

			}

		}

		

		// 日期范围选择

		.date-range {

			flex: 1;

			display: flex;

			gap: 16rpx;

			

			.date-picker {

				flex: 1;

				height: 80rpx;

				background: #ffffff;

				border-radius: 8rpx;

				padding: 0 20rpx;

				display: flex;

				align-items: center;

				justify-content: space-between;

				border: 1rpx solid #e0e0e0;

				

				.date-text {

					font-size: 28rpx;

					color: #999999;

				}

				

				.date-arrow {

					width: 20rpx;

					height: 20rpx;

					transform: rotate(90deg);

				}

			}

		}

	}

}



// 操作按钮区域

.action-section {

	padding: 40rpx 20rpx;

	

	.confirm-btn {

		width: 100%;

		height: 88rpx;

		background: linear-gradient(180deg, #4A90E2 0%, #007AFF 100%);

		color: #ffffff;

		border-radius: 12rpx;

		font-size: 32rpx;

		font-weight: 500;

		border: none;

		

		&:disabled {

			background: #cccccc;

			color: #999999;

		}

	}

}

</style>

